<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use app\models\Morning;
/**
 * This is the model class for table "blxbgame_morning".
 *
 * @property integer $id
 * @property integer $morning
 * @property integer $signdays
 * @property integer $totalscore
 * @property string  $updatetime
 */

class MorningRecord extends ActiveRecord
{
	/**
     * @inheritdoc
     */
     public static function tableName(){
        return "{{%morning_record}}";
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid', 'time', 'score'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '起床表序号',
            'userid' => '用户序号',
            'time' => '签到时间',
            'msectime' => '签到精确时间',
            'score' => '获得积分',
            'signscore' => '连签获得积分'
        ];
    }

    public function add($data){
        $record = new MorningRecord();
        $record->userid = $data['userid'];
        $record->time = $data['time'];
        $record->msectime = $data['msectime'];
        $record->score = $data['score'];
        $record->signscore = $data['signscore'];
        $result = $record->save();
        if(!$result){
            $result = -1;
        }
        return $result;
    }

    public function getTimeRank($date,$time,$msectime){
        $count = MorningRecord::find()->where('(time >= :date and time < :time) OR (time = :time  AND msectime < :msectime) ',[':date'=>$date,':time'=>$time,':msectime'=>$msectime])->count();
        return $count+1;
    }

    public function yesterdaySignup($userid){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y-m-d' );
        $yesterday = date("Y-m-d",time()-24*60*60);
        $morningrecord = MorningRecord::find()->where('time >= :time1 and time < :time2 and userid = :userid',[':userid'=>$userid,':time1'=>$yesterday,':time2'=>$today])->asArray()->one();
        return $morningrecord;
    }

    public function getMorningRecord($userid){
        date_default_timezone_set('Asia/Shanghai');
        $today = date ( 'Y/m/d' );
        $startline = "2019/11/28";
        $timearr = MorningRecord::find()->where( 'userid = :userid',[':userid'=>$userid] )->orderBy('time DESC')->asArray()->all();
        $timedata = NULL;
        $timeout  = NULL;//未签到的日期
        $count = 0;//计数器
        if( !empty($timearr) ){
            $i = 0;
            foreach( $timearr as $value ){
                $datetime = explode(' ', $value['time']);
                $date = $datetime[0];
                $date = explode('-', $date);
                $date = $date[0].'/'.$date[1].'/'.$date[2];
                $time = $datetime[1];
                $hs = explode(':', $time);
                $time = $hs[0] . ":" . $hs[1];
                $rank = MorningRecord::getTimeRank($date,$value['time'],$value['msectime']);
                $score = $value['score'] - $value['signscore'];
                $timedata[$i] = array(
                        'rank' => $rank,
                        'date' => $date,
                        'time' => $time,
                        'score' => $score,
                        'signscore' => $value['signscore'],
                    );
                $i++;
            }
            $i--;
            //先判断最开始的签到与起床开始时间弥补间断
            if($timedata[$i]['date'] != $startline){
                $count = date_diff(date_create($startline),date_create($timedata[$i]['date']))->format("%a");
                //临时时间casualtime
                for($j=0;$j<$count;$j++){
                    $casualtime = NULL;
                    $casualtime = date_create($startline);
                    $diff = $j;
                    date_modify($casualtime,'+'. $diff. ' days');
                    $casualtime = date_format($casualtime,"Y/m/d");
                    $timeout[$j] = array(
                        'date' => $casualtime,
                        'rank' => 0,
                    );
                }
            }
            $count--;
            for($j=$i;$j>0;$j--){
                $timecount = date_diff(date_create($timedata[$j]['date']),date_create($timedata[$j-1]['date']))->format("%a");
                if($timecount > 1){
                    for($k=1;$k<$timecount;$k++){
                        $count++;
                        $casualtime = NULL;
                        $casualtime = date_create($timedata[$j]['date']);
                        date_modify($casualtime,'+'.$k.' days');
                        $casualtime = date_format($casualtime,"Y/m/d");
                        $timeout[$count] = array(
                            'date' => $casualtime,
                            'rank' => 0,
                        );
                    }
                }
            }
           
           if($timedata[0]['date'] != $today){
                $lastcount = date_diff(date_create($timedata[0]['date']),date_create($today))->format("%a");
                for($j=0;$j<$lastcount;$j++){
                    $count++;
                    $casualtime = NULL;
                    $casualtime = date_create($timedata[0]['date']);
                    $diff = $j+1;
                    date_modify($casualtime,'+'. $diff. ' days');
                    $casualtime = date_format($casualtime,"Y/m/d");
                    $timeout[$count] = array(
                        'date' => $casualtime,
                        'rank' => 0,
                    );
                }
            }
         
           if( !empty($timeout) ){
                $timedata = array_merge($timedata,$timeout);
                $count = count($timedata);
                for($i=0;$i<$count;$i++){
                    for($j=$i+1;$j<$count;$j++){
                       if(strtotime($timedata[$i]['date']) < strtotime($timedata[$j]['date'])){
                            $casualarr = $timedata[$i];
                            $timedata[$i] = $timedata[$j];
                            $timedata[$j] = $casualarr;
                            $casualarr = Null;
                       } 
                    }
                }
            }
        } 
        return $timedata;
    }

}
